package com.example.store2.Controller;

import com.example.store2.Service.PublicResourcesService;
import com.example.store2.pojo.Atten;
import com.example.store2.pojo.PageBean;
import com.example.store2.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/publicresource")
public class PublicResourcesController {
    @Autowired
    private PublicResourcesService publicResourcesService;

    @GetMapping
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize, Integer occupationId, Integer classesId, String resourcesName) {
        log.info("公共资源参数{},{},{},{},{}",page,pageSize,occupationId,classesId,resourcesName);
        PageBean pageBean = publicResourcesService.list(page, pageSize, occupationId, classesId, resourcesName);
        return Result.success(pageBean);
    }

    @PostMapping("/{userId}")
    public Result add(@PathVariable Integer userId, @RequestBody Integer resourcesId) {
        log.info("被添加的资源userid:{},resourcesId:{}",userId,resourcesId);
        if(publicResourcesService.add(userId, resourcesId))
            return Result.success();
        else
            return Result.error("该资源已被添加");
    }

    @PostMapping
    public Result addattention(@RequestBody Atten atten) {
        //post以后只接收对象，这么写不规范
        log.info("关注人{}",atten);
        if (publicResourcesService.addattention(atten))
            return Result.success();
        else
            return Result.error("此人已经关注过了");
    }
}
